// mypage/customerSer/customerSer.js
var app=getApp();
var util = require("../../utils/util");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    time: '',
    ser_list:[],
    ser_chat: [""],
    text: '',
    focus: false,
    userinfo: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var time = util.formatTime(new Date());
    that.setData({time: time});
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({userinfo: res.data})
      }
    })
    //
    that.get_problem_list();
  },
  // 展开问题
  open_more:function(e){
    var that = this,ser_list = that.data.ser_list;
    var index = e.currentTarget.dataset.index;
    ser_list[index].open = !ser_list[index].open;
    that.setData({ser_list: ser_list})
  },
  // 用户点击问题
  check_problem:function(e){
    var that=this,list=that.data.ser_list,ser_chat=that.data.ser_chat;
    var index=e.currentTarget.dataset.index,idx=e.currentTarget.dataset.idx;
    var classify=list[index].childList[idx].classify,answer=list[index].childList[idx].problemAnswer;
    ser_chat.push(classify)
    that.setData({ser_chat:ser_chat});
    this.scrll_to();
    setTimeout(() => {
      ser_chat.push(answer)
      that.setData({ser_chat:ser_chat});
      this.scrll_to();
    }, 800);
  },
  scrll_to:function(){
    var query = wx.createSelectorQuery();
    query.selectAll('.page_cont').boundingClientRect(function (res) {
      wx.pageScrollTo({
        scrollTop: res[0].height,
        duration: 300
      })
    }).exec()
  },
  // 输入反馈
  input_val:function(e){
    this.setData({text: e.detail.value})
  },
  input_focus:function(e){
    this.setData({focus: true})
  },
  input_blur:function(){
    this.setData({focus: false,})
  },
  confirm_val:function(e){
    var that=this,userinfo=that.data.userinfo,testSpace = /^[ ]*$/g;
    var val=e.detail.value;
    if(!val||testSpace.test(val)){
      util.page_show_toast('留言不能为空',1500)
      return false;
    }
    var params = {
      "phone": userinfo.phone,
      "content": util.removeAllSpace(val),
      "picList": []
    }
    that.set_feed_back(params);
    that.setData({text: ''})
  },
  send_val:function(){
    var that=this,text=that.data.text,userinfo=that.data.userinfo,testSpace = /^[ ]*$/g;
    if(!text||testSpace.test(text)){
      util.page_show_toast('留言不能为空',1500)
      return false;
    }
    var params = {
      "phone": userinfo.phone,
      "content": util.removeAllSpace(text),
      "picList": []
    }
    that.set_feed_back(params);
    that.setData({text: ''})
  },
  // 问题列表
  get_problem_list:function(){
    wx.showLoading({title: '加载中'})
    var that=this;
    my.get_problem_list({parentId: 1}).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        for(var i=0;i<list.length;i++){
          var num=util.convertToChinaNum(i+1)
          list[i].classifyNum = num
        }
        that.setData({ser_list: list})
      }
    })
  },
  // 反馈请求
  set_feed_back:function(params){
    var that=this;
    my.set_feed_back(params).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        util.page_show_toast('提交成功',1500)
      }
    })
  }
})